package com.firesrc.dao;

import java.util.List;

import jodd.db.DbThreadSession;
import jodd.db.oom.DbOomQuery;
import jodd.db.oom.sqlgen.DbEntitySql;
import jodd.db.pool.CoreConnectionPool;

import com.firesrc.db.DBManager;

public class SuperDao<T> {
	protected CoreConnectionPool cp = DBManager.getPublicConnectionPool();

	public void add(T t) {
		new DbThreadSession(cp);
		DbEntitySql.insert(t).query().executeUpdateAndClose();
	}

	public void save(T t) {
		new DbThreadSession(cp);
		DbEntitySql.update(t).query().executeUpdateAndClose();
	}

	public List<T> getAllList(T t) {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		List<T> list = DbEntitySql.from(t.getClass()).query().list();
		dbThreadSession.closeSession();
		return list;
	}

	public void delete(T t) {
		new DbThreadSession(cp);
		DbOomQuery query = DbEntitySql.deleteById(t).query();
		query.executeUpdateAndClose();
	}

	public void update(T t) {
		new DbThreadSession(cp);
		DbOomQuery query = DbEntitySql.update(t).query();
		query.executeUpdateAndClose();
	}

	public void updateColumn(T t, String column, String value) {
		new DbThreadSession(cp);
		DbOomQuery query = DbEntitySql.updateColumn(t, column, value).query();
		query.executeUpdateAndClose();
	}

	public T getById(T t) {
		new DbThreadSession(cp);
		return (T) DbEntitySql.findById(t).query().find();
	}
}